package com.view;

import com.bean.User;
import com.lovo.netCRM.component.LovoTree;
import com.lovo.netCRM.component.LovoTreeNode;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
import com.view.inventtorymanage.InventtoryManagePanel;
import com.view.jurisdictionmanage.RoleJurManagePanel;
import com.view.jurisdictionmanage.UserJurManagePanel;
import com.view.logmanage.LogManagePanel;
import com.view.personagemanage.PersonageManagePanel;
import com.view.purchasemanage.PurInManagePanel;
import com.view.purchasemanage.PurQueryManagePanel;
import com.view.resourcemanage.CustomerFrame.ClientManagePanel;
import com.view.resourcemanage.SupplierFrame.SupplierManagePanel;
import com.view.sellmanage.SellOutManagePanel;
import com.view.sellmanage.SellQueryManagePanel;
import com.view.statisticsmanage.ClientStatManagePanel;
import com.view.statisticsmanage.SellStatManagePanel;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class MainFrame extends JFrame {

    private Container contentP;

    private LovoTree tree;

    private LovoTreeNode rootNode;//根节点
    private LovoTreeNode resourceMagNode;//资料管理
    private LovoTreeNode supplierNode;
    private LovoTreeNode clientNode;
    private LovoTreeNode purchaseMagNode;//采购管理
    private LovoTreeNode purInNode;
    private LovoTreeNode purQueryNode;
    private LovoTreeNode sellMagNode;//销售管理
    private LovoTreeNode outInNode;
    private LovoTreeNode sellQueryNode;
    private LovoTreeNode inventtoryMagNode;//库存管理
    private LovoTreeNode statisticsMagNode;//统计信息
    private LovoTreeNode sellStatNode;
    private LovoTreeNode clientStatNode;
    private LovoTreeNode jurisdictionMagNode;//权限管理
    private LovoTreeNode userNode;
    private LovoTreeNode roleNode;
    private LovoTreeNode personageMagNode;//个人信息管理
    private LovoTreeNode changePwdNode;
    private LovoTreeNode logMagNode;//日志信息
    private LovoTreeNode logInNode;


    private JPanel cardPanel;
    private WelcomePanel wp;
    private SupplierManagePanel smP;
    private ClientManagePanel cMP;
    private PurQueryManagePanel pQMP;
    private PurInManagePanel pIMP;
    private SellOutManagePanel sOMP;
    private SellQueryManagePanel sQMP;
    private InventtoryManagePanel iMP;
    private SellStatManagePanel sSMP;
    private ClientStatManagePanel cSMP;
    private UserJurManagePanel uJMP;
    private RoleJurManagePanel rJMP;
    private PersonageManagePanel pMP;
    private LogManagePanel lMP;
    private User user;
    private UserService userService = new UserServiceImpl();

    public MainFrame(User user){
        Toolkit tk = Toolkit.getDefaultToolkit();
        this.setIconImage(tk.createImage("image/hp.JPG"));
        this.user = user;
        this.setTitle("进销存管理系统");
        this.setLocation(140, 190);
        this.setSize(1000, 580);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.initTree();
        this.initCard();

        this.setVisible(true);
    }

    private void initTree() {
        // TODO Auto-generated method stub
        this.contentP = this.getContentPane();
//		this.contentP.setBackground(Color.WHITE);
        this.contentP.setLayout(null);


        this.rootNode = new LovoTreeNode("进销存系统管理");
        this.resourceMagNode = new LovoTreeNode("资料管理");
        this.supplierNode = new LovoTreeNode("供应商管理"){
            @Override
            public void  addListener(){
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "supplier");
            }
        };
        this.clientNode = new LovoTreeNode("客户管理"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "client");
            }
        };
        this.purchaseMagNode = new LovoTreeNode("采购管理");
        this.purInNode = new LovoTreeNode("采购入库"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "purIn");
            }
        };
        this.purQueryNode = new LovoTreeNode("采购信息查询"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "purQuery");
            }
        };
        this.sellMagNode = new LovoTreeNode("销售管理");
        this.outInNode = new LovoTreeNode("销售出库"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "sellOut");
            }
        };
        this.sellQueryNode = new LovoTreeNode("销售信息查询"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "sellQuery");
            }
        };
        this.inventtoryMagNode = new LovoTreeNode("库存管理"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "invent");
            }
        };

        this.statisticsMagNode = new LovoTreeNode("统计信息");
        this.sellStatNode = new LovoTreeNode("销售统计"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "sellstat");
            }
        };
        this.clientStatNode = new LovoTreeNode("客户统计"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "clientstat");
            }
        };
        this.jurisdictionMagNode = new LovoTreeNode("权限管理");
        this.userNode = new LovoTreeNode("用户管理"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "user");
            }
        };
        this.roleNode = new LovoTreeNode("角色管理"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "role");
            }
        };
        this.personageMagNode = new LovoTreeNode("个人信息管理");
        this.changePwdNode = new LovoTreeNode("修改密码"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "personage");
            }
        };
        this.logMagNode = new LovoTreeNode("日志信息");
        this.logInNode = new LovoTreeNode("日志一览"){
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                CardLayout cl = (CardLayout)MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "log");
            }
        };

        List<String> powerList = new ArrayList<>();
        powerList = userService.getNowUserPower(user);
        if(powerList.contains("供应商管理")||powerList.contains("客户管理")){
            this.rootNode.add(this.resourceMagNode);
        }
        if(powerList.contains("采购入库")||powerList.contains("采购信息查询")){
            this.rootNode.add(this.purchaseMagNode);
        }
        if(powerList.contains("销售出库")||powerList.contains("销售信息查询")){
            this.rootNode.add(this.sellMagNode);
        }
        if(powerList.contains("客户统计")||powerList.contains("销售统计")){
            this.rootNode.add(this.statisticsMagNode);
        }
        if(powerList.contains("用户管理")||powerList.contains("角色管理")){
            this.rootNode.add(this.jurisdictionMagNode);
        }
        if(powerList.contains("供应商管理")){
            this.resourceMagNode.add(this.supplierNode);
        }
        if(powerList.contains("客户管理")){
            this.resourceMagNode.add(this.clientNode);
        }
        if(powerList.contains("采购入库")){
            this.purchaseMagNode.add(this.purInNode);
        }
        if(powerList.contains("采购信息查询")){
            this.purchaseMagNode.add(this.purQueryNode);
        }
        if(powerList.contains("销售出库")){
            this.sellMagNode.add(this.outInNode);
        }
        if(powerList.contains("销售信息查询")){
            this.sellMagNode.add(this.sellQueryNode);
        }
        if(powerList.contains("库存管理")){
            this.rootNode.add(this.inventtoryMagNode);
        }
        if(powerList.contains("销售统计")){
            this.statisticsMagNode.add(this.sellStatNode);
        }
        if(powerList.contains("客户统计")){
            this.statisticsMagNode.add(this.clientStatNode);
        }
        if(powerList.contains("用户管理")){
            this.jurisdictionMagNode.add(this.userNode);
        }
        if(powerList.contains("角色管理")){
            this.jurisdictionMagNode.add(this.roleNode);
        }
        if(powerList.contains("日志一览")){
            this.rootNode.add(this.logMagNode);
            this.logMagNode.add(this.logInNode);
        }

        this.rootNode.add(this.personageMagNode);
        this.personageMagNode.add(this.changePwdNode);


        this.tree = new LovoTree(this.rootNode);
//        this.tree.setFont(new Font("微软雅黑",Font.ITALIC,20));
        this.tree.setBounds(15, 20, 150, 505);
        this.contentP.add(this.tree);
    }

    private void initCard() {
        // TODO Auto-generated method stub
        this.cardPanel = new JPanel();
        this.cardPanel.setLayout(new CardLayout());

        this.wp = new WelcomePanel();
        this.smP = new SupplierManagePanel(this);
        this.cMP = new ClientManagePanel(this);
        this.pIMP = new PurInManagePanel(this);
        this.pQMP = new PurQueryManagePanel(this);
        this.sOMP = new SellOutManagePanel(this);
        this.sQMP = new SellQueryManagePanel(this);
        this.iMP = new InventtoryManagePanel(this);
        this.sSMP = new SellStatManagePanel(this);
        this.cSMP = new ClientStatManagePanel(this);
        this.uJMP = new UserJurManagePanel(this);
        this.rJMP = new RoleJurManagePanel(this);
        this.pMP = new PersonageManagePanel(this);
        this.lMP = new LogManagePanel(this);

        this.cardPanel.add(this.wp, "welcome");
        this.cardPanel.add(this.smP,"supplier");
        this.cardPanel.add(this.cMP,"client");
        this.cardPanel.add(this.pIMP,"purIn");
        this.cardPanel.add(this.pQMP,"purQuery");
        this.cardPanel.add(this.sOMP,"sellOut");
        this.cardPanel.add(this.sQMP,"sellQuery");
        this.cardPanel.add(this.iMP,"invent");
        this.cardPanel.add(this.sSMP,"sellstat");
        this.cardPanel.add(this.cSMP,"clientstat");
        this.cardPanel.add(this.uJMP,"user");
        this.cardPanel.add(this.rJMP,"role");
        this.cardPanel.add(this.pMP,"personage");
        this.cardPanel.add(this.lMP,"log");


        this.cardPanel.setBounds(170, 20, 800, 505);
        this.contentP.add(this.cardPanel);

    }

    public void flush(){
          smP.flush();
          cMP.flush();
          pQMP.flush();
          pIMP.tableFlush();
          sOMP.cmbFlush();
          sOMP.tableFlush();
          sQMP.flush();
          iMP.flush();
          sSMP.flush();
          cSMP.flush();
          uJMP.flush();
          rJMP.flush();
          lMP.flush();
    }


    public Container getContentP() {
        return contentP;
    }

    public void setContentP(Container contentP) {
        this.contentP = contentP;
    }

    public SupplierManagePanel getSmP() {
        return smP;
    }

    public void setSmP(SupplierManagePanel smP) {
        this.smP = smP;
    }

    public ClientManagePanel getcMP() {
        return cMP;
    }

    public void setcMP(ClientManagePanel cMP) {
        this.cMP = cMP;
    }

    public PurQueryManagePanel getpQMP() {
        return pQMP;
    }

    public void setpQMP(PurQueryManagePanel pQMP) {
        this.pQMP = pQMP;
    }

    public PurInManagePanel getpIMP() {
        return pIMP;
    }

    public void setpIMP(PurInManagePanel pIMP) {
        this.pIMP = pIMP;
    }

    public SellOutManagePanel getsOMP() {
        return sOMP;
    }

    public void setsOMP(SellOutManagePanel sOMP) {
        this.sOMP = sOMP;
    }

    public SellQueryManagePanel getsQMP() {
        return sQMP;
    }

    public void setsQMP(SellQueryManagePanel sQMP) {
        this.sQMP = sQMP;
    }

    public InventtoryManagePanel getiMP() {
        return iMP;
    }

    public void setiMP(InventtoryManagePanel iMP) {
        this.iMP = iMP;
    }

    public SellStatManagePanel getsSMP() {
        return sSMP;
    }

    public void setsSMP(SellStatManagePanel sSMP) {
        this.sSMP = sSMP;
    }

    public ClientStatManagePanel getcSMP() {
        return cSMP;
    }

    public void setcSMP(ClientStatManagePanel cSMP) {
        this.cSMP = cSMP;
    }

    public UserJurManagePanel getuJMP() {
        return uJMP;
    }

    public void setuJMP(UserJurManagePanel uJMP) {
        this.uJMP = uJMP;
    }

    public RoleJurManagePanel getrJMP() {
        return rJMP;
    }

    public void setrJMP(RoleJurManagePanel rJMP) {
        this.rJMP = rJMP;
    }

    public PersonageManagePanel getpMP() {
        return pMP;
    }

    public void setpMP(PersonageManagePanel pMP) {
        this.pMP = pMP;
    }

    public LogManagePanel getlMP() {
        return lMP;
    }

    public void setlMP(LogManagePanel lMP) {
        this.lMP = lMP;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
